package com.example.itcacp.repository.mybatis;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.itcacp.entity.RoleAuthority;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RoleAuthorityMapper extends BaseMapper<RoleAuthority> {

    // 批量插入
    @InsertProvider(type = RoleAuthoritySqlProvider.class, method = "insertBatchSql")
    void insertBatch(@Param("roleAuthorities") List<RoleAuthority> roleAuthorities);

    class RoleAuthoritySqlProvider {
        public String insertBatchSql(@Param("roleAuthorities") List<RoleAuthority> roleAuthorities) {
            StringBuilder sql = new StringBuilder();
            sql.append("INSERT INTO it_role_authority (role_id, authority_id) VALUES ");
            for (int i = 0; i < roleAuthorities.size(); i++) {
                if (i > 0) {
                    sql.append(",");
                }
                sql.append("(#{roleAuthorities[").append(i).append("].roleId}, #{roleAuthorities[").append(i).append("].authorityId})");
            }
            return sql.toString();
        }
    }
}
